package com.bicon.botu.mall.component.service.impl;

import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.component.common.InformationConstant;
import com.bicon.botu.third.provider.rpc.service.MessageRpcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author 张凡
 * @date 2017/12/7 10:10
 */
@Component
@Slf4j
public class SystemInformationPush {
    @Autowired
    private MessageRpcService jpushrpc;
    @Value("${jpush.appkey}")
    String appKey;
    @Value("${jpush.master}")
    String master;
    @Value("${rpc.repeat.times}")
    int rpcRepeatTimes;

    /**
     * 推送系统消息
     *
     * @param
     * @param userIds 多个用户id
     */
    public void systemInformationPush(String[] userIds, Map map) {
        String[] alias = userIds;
        //推送标题
        String title = InformationConstant.PUSH_TITLE;
        String notification_title = (String) map.get("headline");
        for (int i = 0; i < rpcRepeatTimes; i++) {
            BaseResult baseResult = jpushrpc.pushAllTargetByUser(notification_title, title, map, alias, appKey, master);
            if (baseResult.getCode() == 200) {
                break;
            }
            if (i == rpcRepeatTimes - 1) {
                log.info("推送失败");
            }
        }
    }

}
